//
//  TabBarViewController.m
//  tabBar
//
//  Created by 袁量 on 2018/3/21.
//  Copyright © 2018年 JLM. All rights reserved.
//

#import "TabBarViewController.h"
#import "ViewController.h"

@interface TabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    int index = (int)[self.viewControllers indexOfObject:viewController];
    if (self.selectedIndex == 0 && viewController != self.viewControllers[0]) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确认删除？" message:@"清空当前信息" preferredStyle:UIAlertControllerStyleAlert];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            ViewController *vc = (ViewController *)self.viewControllers[0];
            dispatch_async(dispatch_get_main_queue(), ^{
                vc.textField.text = @"";
            });
            self.selectedIndex = index;
        }]];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:alertVC animated:YES completion:nil];
        return NO;
    }
    return YES;
}

@end
